package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.homework.MistakeBook;
import com.fjh.springboot_01.service.MistakeBookService;
import com.fjh.springboot_01.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mistake-book")
public class MistakeBookController {
    @Autowired
    private MistakeBookService mistakeBookService;

    // 获取学生的错题本列表
    @GetMapping("/student/{studentId}")
    public Result<List<MistakeBook>> getStudentMistakes(@PathVariable Integer studentId) {
        try {
            List<MistakeBook> mistakes = mistakeBookService.getStudentMistakes(studentId);
            return Result.success(mistakes);
        } catch (Exception e) {
            return Result.error("获取错题本失败：" + e.getMessage());
        }
    }

    // 从错题本中删除题目
    @DeleteMapping("/{mistakeId}")
    public Result<Void> deleteMistake(@PathVariable Integer mistakeId) {
        try {
            // 这里需要添加一个删除方法到Service和Mapper中
            mistakeBookService.deleteMistake(mistakeId);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除错题失败：" + e.getMessage());
        }
    }

    // 更新错题记录
    @PutMapping("/{mistakeId}")
    public Result<Void> updateMistake(
            @PathVariable Integer mistakeId,
            @RequestBody MistakeBook mistakeBook
    ) {
        try {
            mistakeBook.setId(mistakeId);
            mistakeBookService.updateMistake(mistakeBook);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新错题记录失败：" + e.getMessage());
        }
    }

    // 更新重做次数
    @PutMapping("/{mistakeId}/retry")
    public Result<Void> updateRetryCount(
            @PathVariable Integer mistakeId,
            @RequestParam boolean isCorrect
    ) {
        try {
            mistakeBookService.updateRetryCount(mistakeId, isCorrect);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新重做记录失败：" + e.getMessage());
        }
    }

    // 获取学生的错题知识点统计
    @GetMapping("/student/{studentId}/knowledge-stats")
    public Result<List<Map<String, Object>>> getKnowledgeStats(@PathVariable Integer studentId) {
        try {
            List<Map<String, Object>> stats = mistakeBookService.getKnowledgeStats(studentId);
            return Result.success(stats);
        } catch (Exception e) {
            return Result.error("获取知识点统计失败：" + e.getMessage());
        }
    }
} 